<template>
	<view class="level-price-container">
		<u-navbar :border-bottom="false" title="管理价格" :background="{'background':'#fff'}" :title-size="36" :title-bold="true" :title-color="textColor">
		</u-navbar>
		<view class="payorder-section">
			<image :src="productDetail.resourceUrl?productDetail.resourceUrl:defaultLogo" mode="" class="section-image" ></image>
			<view class="section-list ">
				<view class="section-productDetail u-m-l-10">
					<view class="section-productDetail-name u-font-30" style="width:100%;font-weight: 500;">
					{{productDetail.name}}
					</view>
					<view class="u-font-24 u-m-t-10 " v-if="productDetail.specification" style=" display: inline-block; color: #999;background-color: #f8f8f8;padding: 10rpx 12rpx;border-radius: 10rpx;">
						{{productDetail.specification}}
					</view>
				</view>
			</view>
			<view class="u-flex u-col-center u-row-between u-m-l-20" style="height: 100%;">
				<view class="section-btn-box " >
					{{productDetail.unit||'瓶'}}
				</view>
			</view>
		</view>
		<view class="u-m-t-20 u-m-b-20 u-p-16 u-flex " style="background-color: #fff;color:#999;align-items: flex-start;">
				<text class="iconfont icon-zhuyi u-m-r-14 u-m-t-4 u-font-28" color="#333"></text>
				<view class="u-font-28">
					级别值越小价格越低
				</view>
		</view>
		<view class="">
			<view class="u-m-t-20 u-m-b-20 u-p-t-16 u-p-b-16 u-p-l-35 u-p-r-35 u-flex u-row-between" style="background-color: #fff;" v-for="item in levelList" :key="item.id">
				<view class="u-flex">
					<view class="u-flex u-row-center u-m-r-20" style="width: 80rpx;height: 80rpx;border-radius: 50%;background-color: #f8f8f8;">
						<text class="u-font-44" style="font-weight: bold;">{{item.levelValue}}</text>
					</view>
					<view class="u-font-28">
						{{item.name}}
					</view>
				</view>
				<view class="u-text-right">
					<text class="u-font-40" style="color: #ff551a;font-weight: 500;" v-if="!editPrice">{{item.price?'￥'+item.price:'未配置'}}</text>
					<view class="u-flex" v-if="editPrice">
						￥  <u-input type="number" input-align="center"	 v-model="item.price" placeholder="输入价格" :clearable="false" border style="width: 200rpx;text-align: center;"></u-input>
					</view>
				</view>
			</view>
		</view>
		<view class="u-flex u-p-l-35 u-p-r-35" style="height: 200rpx; width: 100%;position: fixed;bottom: 0;left: 0;background-color: #fff;">
			<u-button :hair-line="false" :throttle-time="0"	 hover-class="none" v-if="!editPrice"  @click="editPrice = true;" style="width: 100%;height: 90rpx;border-radius: 50rpx;background-color: #ffbb1a;border:none">修改</u-button>
			<u-button :hair-line="false" :throttle-time="0"	 hover-class="none" v-if="editPrice" @click="editPrice = false;" style="width: 315rpx;height: 90rpx;border-radius: 50rpx;">取消</u-button>
			<u-button :hair-line="false" hover-class="none" @click="savePriceItemFn" v-if="editPrice" style="width: 315rpx;height: 90rpx;border-radius: 50rpx;background-color: #ffbb1a;border:none">保存</u-button>
		</view>
		<!-- 提交按钮 -->
		<akLoading isFullScreen :active="loading"></akLoading>
	</view>
</template>

<script>
	import { useShopStore } from '@/store/shop.js';
	import { mapState } from 'pinia';
	export default {
		data(){
			return{
				productDetail:{},
				levelList:[],
				loading:false,
				editPrice:false
			}
		},
		computed:{
			...mapState(useShopStore, {
				shopId(store){
					return store.shop.id;
				}
			})
		},
		onLoad(options) {
			if(options.skuId){
				this.productDetail = JSON.parse(options.item+options.skuId);
			}else{
				this.productDetail = JSON.parse(options.item);
			}
			this.getLevelsFn();
		},
		methods:{
			async getLevelsFn(){
				this.loading = true;
				let params = {
					isDeleted:false,
					pageIndex:1,
					pageSize:999,
					shopId:this.shopId
				}
				const res = await this.$setHttp.post('api/BrandOwner/GetShopLevelsByPage', params,{header:{shopId:this.shopId},  });
				if(res.code === 200){
					let ary = res.data.list.map(item=>{
						return item.id;
					})
					const priceList = await this.getPriceByLevelsFn(ary);
					this.levelList = res.data.list.map((item,index)=>{
						item.price = priceList[index].price;
						return item;
					});
				}else{
					this.loading = false;
					this.$u.toast('获取级别列表失败');
				}
			},
			async getPriceByLevelsFn(ary){
				let params = {
					id:this.productDetail.id,
					shopId:this.shopId,
					shopLevelIds:ary
				}
				const res = await this.$setHttp.post('api/BrandOwner/GetPricesByItem', params,{header:{shopId:this.shopId},  });
				this.loading = false;
				if(res.code === 200){
					// resolve(res.data.list)
					return res.data
				}else{
					this.$u.toast('获取级别价格失败');
				}
			},
			async savePriceItemFn(){
				this.loading = true;
				let ary = 
				this.levelList.map(item=>{
					return {
						itemId:this.productDetail.id,
						price:item.price,
						shopLevelId:item.id
					}
				})
				let params = {
					itemPrices:ary,
					shopId:this.shopId
				}
				const res = await this.$setHttp.post('api/BrandOwner/SaveItemPrices', params,{header:{shopId:this.shopId},  });
				this.loading = false;
				if(res.code === 200){
					this.$u.toast('修改级别价格成功');
					this.editPrice = false;
				}else{
					this.$u.toast('修改级别价格失败');
				}
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import '@/static/iconfont/iconfont.css';
	.level-price-container{
		position: relative;
		box-sizing: border-box;
		padding-bottom: 200rpx;
		.payorder-section{
			display: flex;
			align-items: center;
			box-sizing: border-box;
			padding: 42rpx 35rpx;
			margin-bottom: 20rpx;
			background-color: #fff;
			.section-image{
				width: 100rpx;
				height:100rpx;
				display: block;
				border-radius: 10rpx;
				margin-right: 32rpx;
			}
			.section-list{
				flex: 1;
				overflow: hidden;
				// margin-left: 20rpx;
				.section-btn{
					width: 160rpx;
					margin-left: 10rpx;
					height:70rpx;
					border-radius: 35rpx;
					border: 1rpx solid #FFBB1A;
					color: #FFBB1A;
					text-align: center;
					line-height: 70rpx;
				}
				.section-btn-disabled{
					border-color:#999999;
					color: #999999;
				}
				.section-productDetail{
					width: 100%;
					.section-productDetail-name{
						font-size: 32rpx;
						font-weight: 500;
					}
					.section-productDetail-count{
						font-size: 24rpx;
						color: #999;
					}
					
				}
			}
			.section-btn-box{
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				background-color: #FF551A;
				color: #fff;
				width: 54rpx;
				height: 54rpx;
				border-radius: 10rpx;
				>.btn-text{
					font-size: 20rpx;
					margin-top: 10rpx;
				}
			}
			
		}
	}
</style>